﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using BADDGUI.Utils.NETHelper;
using BADDGUI.Utils.DTHelper;
using System.Reflection;

namespace BADDGUI
{
    public partial class frmFmvm : BLCForm
    {
        private string commasepcolumns = "";

        #region Constructor & Load

        public frmFmvm()
        {
            InitializeComponent();
            InitializeUI();
        }

        protected override void OnLoad(EventArgs e)
        {
            InitializeData();
            base.OnLoad(e);
        }

        #endregion

        #region Event

        private void frmFmvm_Shown(object sender, EventArgs e)
        {
            this.DataGrid.SetColumnOrder();
            this.ViewFilter.InitializeData();
        }

        private void frmFmvm_Leave(object sender, EventArgs e)
        {
            this.DataGrid.SaveColumnOrder();
        }

        int oldval = 0;
        int newVal = 0;
        //private void dgvScrollBar_Scroll(object sender, ScrollEventArgs e)
        //{
        //    txtValue.Text = e.NewValue.ToString();
        //    oldval = e.OldValue;
        //    newVal = e.NewValue;
        //}

        bool scrollDone = false;
        ScrollEventArgs myCurrentScrEArg;
        private void DataGrid_Scrolled(object sender, ScrollEventArgs e)
        {
            myCurrentScrEArg = e;
            if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
            {
                if (this.DataGrid.Rows.Count < this.RowCount)
                {
                    if (this.DataGrid.Rows[this.DataGrid.Rows.Count - 1].Displayed)
                    {
                        int TrxNo = (int)this.DataGrid.Rows[this.DataGrid.Rows.Count - 1].Cells[0].Value;
                        DataTable dtNew = retriever.SupplyPageOfData("Trx No", TrxNo, 50);
                        oldval = e.OldValue;
                        dtRow.Merge(dtNew, false);
                        e.NewValue = oldval;
                        newVal = e.NewValue;
                        scrollDone = true;
                    }
                }
            }
        }

        private void DataGrid_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            if (this.DataGrid.Rows.Count >= 50)
            {
                if (e.Row == this.DataGrid.Rows[this.DataGrid.Rows.Count - 1])
                {
                    if (e.Row.Displayed && scrollDone == false)
                    {
                        DataGrid_Scrolled(null, myCurrentScrEArg);
                    }
                }
            }
        }

        #endregion

        #region Method

        public void InitializeUI()
        {
            InitDataGrid();
            this.ViewFilter = new Controls.BLCViewToolStrip(this.DataGrid);
            pnlViewFilter.Controls.Add(this.ViewFilter);
        }

        DataTable dtRow;
        BADDGUI.Utils.DTHelper.DataRetriever retriever;
        public void InitializeData()
        {
            retriever = new BADDGUI.Utils.DTHelper.DataRetriever(this.FormView, commasepcolumns, "", new List<object>(), new List<string>());
            dtRow = retriever.SupplyPageOfData("Trx No", 0, 50);
            this.DataGrid.DataSource = dtRow;

            this._RowCount = (int)BLCGlobalData.BASLOG.GetAVal("SELECT COUNT(*) FROM " + this.FormView);
        }

        public void InitDataGrid()
        {
            this.DataGrid = new BADDGUI.Controls.BLCDataGridView(this);
            this.DataGrid.BorderStyle = BorderStyle.None;
            this.DataGrid.ReadOnly = true;
            this.DataGrid.AllowUserToAddRows = false;
            this.DataGrid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //this.DataGrid.Scroll += dgvScrollBar_Scroll;
            this.DataGrid.Scroll += DataGrid_Scrolled;
            this.DataGrid.RowStateChanged += DataGrid_RowStateChanged;

            ((System.ComponentModel.ISupportInitialize)(this.DataGrid)).BeginInit();
            this.SuspendLayout();

            DataTable dtSchema = BLCGlobalData.BASLOG.GetAViewSchema(this.FormView);

            int i = 0;
            foreach (DataRow row in dtSchema.Rows)
            {
                i++;
                DataGridViewColumn mydgvCol = new DataGridViewTextBoxColumn();
                commasepcolumns += (commasepcolumns == "" ? string.Empty : ", ") + '[' + row["ColumnName"].ToString() + ']';
                mydgvCol.Name = "col" + row["ColumnName"].ToString().Replace(" ", string.Empty);
                mydgvCol.HeaderText = row["ColumnName"].ToString();
                mydgvCol.DataPropertyName = row["ColumnName"].ToString();
                mydgvCol.ValueType = Type.GetType(row["DataType"].ToString());
                this.DataGrid.Columns.Add(mydgvCol);
                if (i > 30) break;
            }

            blcPnlGrid.Controls.Add(this.DataGrid);

            ((System.ComponentModel.ISupportInitialize)(this.DataGrid)).EndInit();
            this.ResumeLayout(false);
        }

        #endregion

        #region Property

        public Object DataSource
        {
            get { return this.DataGrid.DataSource; }
            set { this.DataGrid.DataSource = value; }
        }

        private int _RowCount;
        public int RowCount
        {
            get { return _RowCount; }
            set { _RowCount = value; }
        }

        #endregion
    }
}